package com.pdg.dungeondelver.engine.propertyserializers;

import org.newdawn.slick.util.xml.XMLElement;
import org.newdawn.slick.util.xml.XMLElementList;

import com.pdg.dungeondelver.configuration.Configuration;
import com.pdg.dungeondelver.configuration.PropertySerializer;
import com.pdg.dungeondelver.engine.WeightedGenerator;

public class IntegerGeneratorPropertySerializer implements PropertySerializer{

	private static final String TAG_KEY = "key";
	private static final String TAG_VALUE = "weight";

	@Override
	public Object deserialize(XMLElement element, Configuration configuration) {
		WeightedGenerator<Integer> result = new WeightedGenerator<Integer>();
		XMLElementList children = element.getChildren();
		for(Integer index=0;index<children.size();++index){
			XMLElement child = children.get(index);
			Integer key = Integer.parseInt(child.getChildrenByName(TAG_KEY).get(0).getContent());
			Integer value = Integer.parseInt(child.getChildrenByName(TAG_VALUE).get(0).getContent());
			result.setWeight(key, value);
		}
		return result;
	}

}
